package cn.sytton.taffe.web.flw.controller;

import cn.sytton.taffe.excel.annotation.ExportExcel;
import cn.sytton.taffe.framework.base.entity.PageParam;
import cn.sytton.taffe.web.flw.entity.FlowTaskVariableEntity;
import cn.sytton.taffe.web.flw.entity.FormInstanceEntity;
import cn.sytton.taffe.web.flw.entity.FlowInstanceVariableEntity;
import cn.sytton.taffe.web.flw.service.IFormInstanceService;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.sytton.taffe.framework.base.controller.BaseController;

/**
 * <p>
 * 表单实例 前端控制器
 * </p>
 *
 * @author rock
 * @since 2023-09-27
 */
@Tag(name = "表单实例")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flw/form-instance")
public class FormInstanceController extends BaseController {

    private final IFormInstanceService formInstanceService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "查询分页列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @ExportExcel
    @GetMapping
    public PageInfo<FormInstanceEntity> findPage(PageParam pp) {
        return formInstanceService.findPage(pp);
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "查询单个对象")
    @Parameter(name = "id", description = "主键ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/{id}")
    public FormInstanceEntity findById(@PathVariable String id) {
        return formInstanceService.findById(id);
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "保存")
    @PostMapping
    public void save(@RequestBody @Validated FormInstanceEntity entity) {
        formInstanceService.createOrUpdate(entity);
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "删除", description = "body: 主键ID数组")
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        formInstanceService.delete(ids);
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "启动流程")
    @PostMapping("/start")
    public void start(@RequestBody @Validated FlowInstanceVariableEntity entity) {
        formInstanceService.start(entity);
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "审批")
    @PostMapping("/approve")
    public void approve(@RequestBody @Validated FlowTaskVariableEntity entity) {
        formInstanceService.approve(entity);
    }

    @ApiOperationSupport(order = 7)
    @Operation(summary = "查询我申请的分页列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @ExportExcel
    @GetMapping("/my-apply/page")
    public PageInfo<FormInstanceEntity> findMyApplyPage(PageParam pp) {
        return formInstanceService.findMyApplyPage(pp);
    }

    @ApiOperationSupport(order = 8)
    @Operation(summary = "查询我参与审批的分页列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @ExportExcel
    @GetMapping("/my-approve/page")
    public PageInfo<FormInstanceEntity> findMyApprovePage(PageParam pp) {
        return formInstanceService.findMyApprovePage(pp);
    }
}
